// 设置管理器
var SettingsManager = {
  modal: null,
  languageSelect: null,
  
  // 初始化设置管理器
  init: function() {
    this.modal = document.getElementById('settings-modal');
    this.languageSelect = document.getElementById('language-select');
    
    // 绑定事件
    this.bindEvents();
    
    // 设置当前语言选择
    this.updateLanguageSelect();
  },
  
  // 绑定事件
  bindEvents: function() {
    // 设置按钮点击事件
    var settingsButton = document.getElementById('settings-button');
    if (settingsButton) {
      settingsButton.addEventListener('click', this.showModal.bind(this));
    }
    
    // 关闭按钮点击事件
    var closeButton = document.getElementById('close-settings');
    if (closeButton) {
      closeButton.addEventListener('click', this.hideModal.bind(this));
    }
    
    // 点击模态框外部关闭
    if (this.modal) {
      this.modal.addEventListener('click', function(e) {
        if (e.target === this.modal) {
          SettingsManager.hideModal();
        }
      });
    }
    
    // 语言选择变化事件
    if (this.languageSelect) {
      this.languageSelect.addEventListener('change', function(e) {
        var selectedLang = e.target.value;
        LanguageManager.switchLanguage(selectedLang);
        SettingsManager.updateLanguageSelect();
      });
    }
  },
  
  // 显示设置弹窗
  showModal: function() {
    if (this.modal) {
      this.modal.style.display = 'block';
      // 更新弹窗中的文本
      this.updateModalText();
    }
  },
  
  // 隐藏设置弹窗
  hideModal: function() {
    if (this.modal) {
      this.modal.style.display = 'none';
    }
  },
  
  // 更新语言选择器
  updateLanguageSelect: function() {
    if (this.languageSelect) {
      this.languageSelect.value = LanguageManager.currentLang;
    }
  },
  
  // 更新弹窗中的文本
  updateModalText: function() {
    var settingsTitle = document.getElementById('settings-title');
    var languageLabel = document.getElementById('language-label');
    var closeButton = document.getElementById('close-settings');
    
    if (settingsTitle) {
      settingsTitle.textContent = LanguageManager.getText('settings');
    }
    
    if (languageLabel) {
      languageLabel.textContent = LanguageManager.getText('language');
    }
    
    if (closeButton) {
      closeButton.title = LanguageManager.getText('close');
    }
  }
};

// 页面加载完成后初始化
document.addEventListener('DOMContentLoaded', function() {
  // 初始化语言管理器
  LanguageManager.init();
  
  // 初始化设置管理器
  SettingsManager.init();
  
  // 将设置按钮和设置管理器设为全局变量，供语言管理器使用
  window.settingsButton = document.getElementById('settings-button');
  window.SettingsManager = SettingsManager;
});
